<?php


namespace App\Api\Controllers;


use App\Api\Services\JsonService;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use App\Api\Repositories\CommentRepositorie;

class RobotController
{
    public function robot(Request $request)
    {
        //接收参数
        $valadate = $request->validate([
            'say'=>['required'],
        ]);
        //敏感词过滤
        if($valadate['say']=='人工客服'){
            return JsonService::success('访问链接联系',
                                        'http://desc.zhangyange.cn/index/index/user?kefu_code=kefu6072d64c20a17',
                                              config('code.HTTP_OK')
                                        );
        }
        $say = CommentRepositorie::say($valadate['say']);
        if ($say['showapi_res_body']['remark'] == '检测成功') {
            //存在
            return  JsonService::fail('违规语言','',config('code.HTTP_FAIL'));
        } else {
            //不存在
            $client = new Client(['timeout' => 5,'verify' => false]);
            $url = sprintf(config('robot'),$valadate['say']);
            $response = $client->get($url);
            $json = (string)$response->getBody();
            $resay = json_decode($json,true)['content'];
            return JsonService::success('成功',$resay,config('code.HTTP_OK'));
        }
    }

}